class Solution
{
public:
    int maxSum(vector<int> &nums, int k)
    {
        vector<int> bitCount(32, 0);
        for (int num : nums)
        {
            for (int i = 0; i < 32; ++i)
            {
                if (num & (1 << i))
                {
                    bitCount[i]++;
                }
            }
        }

        long long result = 0;
        for (int _ = 0; _ < k; ++_)
        {
            long long num = 0;
            for (int i = 0; i < 32; ++i)
            {
                if (bitCount[i] > 0)
                {
                    num |= (1 << i);
                    bitCount[i]--;
                }
            }
            result += num * num;
            result %= 1000000007;
        }
        return result;
    }
};